package cn.org.xuanzp.examples.v2.server;

import cn.org.xuanzp.accept.handler.AcceptCommandHandler;
import cn.org.xuanzp.accept.provider.AcceptProvider;
import cn.org.xuanzp.accept.provider.impl.LoginProvider;
import cn.org.xuanzp.accept.provider.impl.LoginoutProvider;
import cn.org.xuanzp.accept.provider.impl.MessageProvider;
import cn.org.xuanzp.nty.server.NettyServer;
import cn.org.xuanzp.nty.server.handler.CommandHandler;
import cn.org.xuanzp.nty.server.handler.NtyHandler;
import cn.org.xuanzp.nty.server.handler.TransmitCommandHandler;
import cn.org.xuanzp.send.handler.RouteCommandHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname Server
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/9/24 22:47
 * @Created by xuanzp
 */
public class Server {
    public static void main(String[] args) throws InterruptedException {

        List<AcceptProvider> providers = new ArrayList<>();


        providers.add(new MessageProvider(null));
        providers.add(new LoginoutProvider());
        providers.add(new LoginProvider());

        start( providers);

    }

    private static void start(List<AcceptProvider> providers) throws InterruptedException {
        CommandHandler sendCommandHandler = new RouteCommandHandler();
        CommandHandler transmitCommandHandler = new TransmitCommandHandler();

        NtyHandler ntyHandler = new NtyHandler(sendCommandHandler,transmitCommandHandler);

        AcceptCommandHandler commandHandler = new AcceptCommandHandler(ntyHandler);

        for (AcceptProvider provider : providers) {
            if(provider instanceof MessageProvider){
                ((MessageProvider)provider).setAcceptCommandHandler(commandHandler);
            }
            commandHandler.addProvider(provider);
        }

        NettyServer nettyServer = new NettyServer(8848,commandHandler);
        nettyServer.start();

    }
}
